package com.gitee.wsl.common.ui.ext

import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.TextLayoutResult
import androidx.compose.ui.text.TextMeasurer
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.Constraints

fun TextMeasurer.measure(
    text: CharSequence,
    style: TextStyle,
    overflow: TextOverflow,
    maxLines: Int,
    width: Int,
    height: Int,
): TextLayoutResult {
    val constraints = Constraints(maxWidth = width, maxHeight = height)
    return if (text is AnnotatedString) {
        measure(
            text = text,
            style = style,
            overflow = overflow,
            maxLines = maxLines,
            constraints = constraints,
        )
    } else {
        measure(
            text = text.toString(),
            style = style,
            overflow = overflow,
            maxLines = maxLines,
            constraints = constraints,
        )
    }
}

val TextLayoutResult.bounds: Rect
    get() = Rect(0f, 0f, size.width.toFloat(), size.height.toFloat())
